package cn.iocoder.devlop.module.annual.report.supbaseinfo.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import cn.iocoder.devlop.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.devlop.module.annual.report.baseinfo.dal.dataobject.BaseinfoDO;
import cn.iocoder.devlop.module.annual.report.baseinfo.service.BaseinfoService;
import cn.iocoder.devlop.module.annual.report.supbaseinfo.controller.vo.SupBaseinfoCreateReqVO;
import cn.iocoder.devlop.module.annual.report.supbaseinfo.controller.vo.SupBaseinfoOverwriteVO;
import cn.iocoder.devlop.module.annual.report.supbaseinfo.convert.SupBaseinfoConvert;
import cn.iocoder.devlop.module.annual.report.supbaseinfo.dal.SupBaseinfoMapper;
import cn.iocoder.devlop.module.annual.report.supbaseinfo.dal.dataobject.SupBaseinfoDO;
import cn.iocoder.devlop.module.annual.report.utils.IdGenerateUtil;
import cn.iocoder.devlop.module.annual.report.utils.ObjectComparisonUtil;
import cn.iocoder.devlop.module.annual.report.utils.ReportYearHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

/**
 * 年报基本信息补充 Service 实现类
 *
 * @author 超级管理员
 */
@Service
@Validated
public class SupBaseinfoServiceImpl implements SupBaseinfoService {

    @Resource
    private SupBaseinfoMapper supBaseinfoMapper;


    @Override
    public List<SupBaseinfoDO> getSupBaseinfoList(String regno, String ancheyear) {
        ReportYearHelper.setYear(NumberUtil.parseInt(ancheyear));
        String ancheid = IdGenerateUtil.getAncheid(regno, ancheyear);
        return supBaseinfoMapper.selectListByAncheid(ancheid);
    }

    @Override
    public void deleteSupBaseinfoAll(String regno, String ancheyear) {
        ReportYearHelper.setYear(NumberUtil.parseInt(ancheyear));
        String ancheid = IdGenerateUtil.getAncheid(regno, ancheyear);
        LambdaQueryWrapperX<SupBaseinfoDO> wrapperX = new LambdaQueryWrapperX<>();
        wrapperX.eq(SupBaseinfoDO::getAncheid,ancheid);
        supBaseinfoMapper.delete(wrapperX);
    }

    @Override
    public void createSupBaseinfoByList(String regno, String ancheyear, List<SupBaseinfoDO> supBaseinfoList) {
        ReportYearHelper.setYear(NumberUtil.parseInt(ancheyear));
        String ancheid = IdGenerateUtil.getAncheid(regno, ancheyear);
        if (CollUtil.isNotEmpty(supBaseinfoList)) {
            for (SupBaseinfoDO supBaseinfo : supBaseinfoList) {
                supBaseinfo.setAncheid(ancheid);
            }
            supBaseinfoMapper.insertBatch(supBaseinfoList);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void overwriteSupBaseinfo(SupBaseinfoOverwriteVO updateReqVO) {
        BaseinfoService baseinfoService = SpringUtil.getBean(BaseinfoService.class);
        BaseinfoDO baseinfo = baseinfoService.getBaseinfo(updateReqVO.getRegno(), updateReqVO.getAncheyear());
        if (ObjUtil.isNotEmpty(baseinfo)){
            if (StrUtil.equals(baseinfo.getState(),"02") ||StrUtil.equals(baseinfo.getState(),"03")) {
                List<SupBaseinfoDO> oldSupBaseinfoList = getSupBaseinfoList(updateReqVO.getRegno(), updateReqVO.getAncheyear());
                List<SupBaseinfoDO> newSupBaseinfoList = updateReqVO.getSupBaseinfoList();
                List<SupBaseinfoCreateReqVO> oldreqs = SupBaseinfoConvert.INSTANCE.convertList3(oldSupBaseinfoList);
                List<SupBaseinfoCreateReqVO> newreqs = SupBaseinfoConvert.INSTANCE.convertList3(newSupBaseinfoList);
                ObjectComparisonUtil.compareObjectList(oldreqs,newreqs,IdGenerateUtil.getAncheid(updateReqVO.getRegno(), updateReqVO.getAncheyear()),"股权变更");
            }
        }
        deleteSupBaseinfoAll(updateReqVO.getRegno(), updateReqVO.getAncheyear());
        createSupBaseinfoByList(updateReqVO.getRegno(), updateReqVO.getAncheyear(), updateReqVO.getSupBaseinfoList());
    }

    @Override
    public SupBaseinfoDO getSupBaseinfo(String regno, String ancheyear) {
        ReportYearHelper.setYear(NumberUtil.parseInt(ancheyear));
        String ancheid = IdGenerateUtil.getAncheid(regno, ancheyear);
        return supBaseinfoMapper.selectOne(SupBaseinfoDO::getAncheid, ancheid);
    }


}
